package com.android.svmc.app.mobile.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.svmc.app.mobile.activity.DetailPostActivity;
import com.android.svmc.app.mobile.activity.R;
import com.android.svmc.app.mobile.activity.SettingMainActivity;
import com.android.svmc.app.mobile.util.CommonDefine;
import com.android.svmc.app.mobile.util.SettingUtils;

public class CustomDialog extends Dialog implements
		android.view.View.OnClickListener, CommonDefine {
	private Context context;
	private int dialogType;
	private TextView txt_label;
	private TextView txt_message;
	private Button btn_ok;
	private Button btn_cancel;
	private EditText form_name_comment;
	private String contentComment;

	public CustomDialog(Context context) {
		super(context);
		this.context = context;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.argb(0, 255, 255, 255)));
	}

	public void setDialogType(int dialogType, String message) {
		this.dialogType = dialogType;
		switch (dialogType) {
		case DIALOG_CONFIRM :
			this.setContentView(R.layout.dialog_info);
			txt_label =(TextView)findViewById(R.id.dialoginfo_txt_label);
			txt_message=(TextView)findViewById(R.id.dialoginfo_txt_message);
			btn_ok =(Button)findViewById(R.id.dialoginfo_btn_ok);
			txt_label.setText(context.getString(R.string.label_submit_name_comment));
			txt_message.setText(context.getString(R.string.info_submit_comment_success));
			btn_ok.setOnClickListener(this);
			
			break;
		case DIALOG_WARING_DELETE_DIARY:
		case DIALOG_INFO_NETWORK:
			this.setContentView(R.layout.dialoginfo_network);
			txt_label = (TextView) findViewById(R.id.dialoginfo_network_txt_label);
			txt_message = (TextView) findViewById(R.id.dialoginfo_network_txt_message);
			btn_ok = (Button) findViewById(R.id.dialoginfo_network_btn_setting);
			btn_cancel = (Button) findViewById(R.id.dialoginfo_network_btn_cancel);
			txt_label.setText(context
					.getString(R.string.label_dialog_info_network));
			txt_message.setText(message);
			btn_ok.setOnClickListener(this);
			btn_cancel.setOnClickListener(this);
			break;
		case DIALOG_SUBMIT_NAME_COMMENT:
			this.setContentView(R.layout.dialog_submit_name);
			form_name_comment = (EditText) findViewById(R.id.dialog_submit_name_edt_name);
			btn_ok = (Button) findViewById(R.id.dialog_submit_name_btn_ok);
			btn_ok.setOnClickListener(this);
			contentComment = message;
			form_name_comment.setText(SettingUtils.getStringPreferences(context, ACCOUNT_NAME_DISPLAY));
			break;
		case DIALOG_INFO:
			this.setContentView(R.layout.dialog_info);
			txt_label =(TextView)findViewById(R.id.dialoginfo_txt_label);
			txt_message =(TextView)findViewById(R.id.dialoginfo_txt_message);
			btn_ok =(Button)findViewById(R.id.dialoginfo_btn_ok);
			txt_label.setText(context.getString(R.string.label_error));
			txt_message.setText(message);
			btn_ok.setOnClickListener(this);
			break;
		case DIALOG_WARING_ROTATION :
			this.setContentView(R.layout.dialog_waring_rotation);
			txt_label =(TextView)findViewById(R.id.dialog_waring_txt_label);
			txt_message =(TextView)findViewById(R.id.dialog_waring_txt_message);
			btn_ok =(Button)findViewById(R.id.dialog_waring_btn_accept);
			btn_cancel =(Button)findViewById(R.id.dialog_waring_btn_cancel);
			txt_label.setText(context.getString(R.string.label_dialog_waring));
			txt_message.setText(message);
			btn_cancel.setOnClickListener(this);
			btn_ok.setOnClickListener(this);
			break;
		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		switch (dialogType) {
		
		case DIALOG_CONFIRM:
			if(v.getId()==R.id.dialoginfo_btn_ok){
				DetailPostActivity.form_input_comment.clearComposingText();
				DetailPostActivity.form_input_comment.setText("");
				this.dismiss();
			}
			break;
		case DIALOG_INFO_NETWORK:
			if (v.getId() == R.id.dialoginfo_network_btn_setting) {
				context.startActivity(new Intent(
						android.provider.Settings.ACTION_WIFI_SETTINGS));
			}
			if (v.getId() == R.id.dialoginfo_network_btn_cancel) {
				this.dismiss();
				((Activity) context).finish();
			}
			break;
		case DIALOG_SUBMIT_NAME_COMMENT:
			if (v.getId() == R.id.dialog_submit_name_btn_ok) {
				
				if(form_name_comment.getText().toString().equalsIgnoreCase("")){
					Toast.makeText(context, "Trường tên không được bỏ trống", Toast.LENGTH_LONG).show();
				}else {
					SettingUtils.saveStringPreferences(context, ACCOUNT_NAME_DISPLAY, form_name_comment.getText().toString());
					String name = SettingUtils.getStringPreferences(context, ACCOUNT_NAME_DISPLAY);
					if(name.length() >= 10){
						if(SettingMainActivity.txt_account_name !=null){
							SettingMainActivity.txt_account_name.setText(name.subSequence(0, 10)+"...");
						}						
					}else {
						if(SettingMainActivity.txt_account_name !=null){
							SettingMainActivity.txt_account_name.setText(name);
						}
					}					
					this.dismiss();						
				}
			}
			break;
		case DIALOG_WARING_ROTATION:
			if(v.getId() == R.id.dialog_waring_btn_accept){
				SettingUtils.savebooleanPreferences(context, IS_ROTATION_SCREEN, true);
				SettingMainActivity.cb_rotation.setChecked(SettingUtils.getbooleanPreferences(context, IS_ROTATION_SCREEN));
				this.dismiss();
			}
			if(v.getId() == R.id.dialog_waring_btn_cancel){
				SettingMainActivity.cb_rotation.setChecked(SettingUtils.getbooleanPreferences(context, IS_ROTATION_SCREEN));
				this.dismiss();
			}
			break;
		case DIALOG_INFO:
			if(v.getId() == R.id.dialoginfo_btn_ok){
				this.dismiss();
			}
			break;
		case DIALOG_WARING_DELETE_DIARY:
			if (v.getId() == R.id.dialoginfo_network_btn_setting){
				
			}
			if(v.getId()== R.id.dialoginfo_network_btn_cancel){
				
			}
		default:
			break;

		}
	}
}
